package com.infoland.afanmall.base.util;

import java.math.BigDecimal;

/**
 * Created by geely
 */
public class DecimalKit {
    private DecimalKit() {

    }

    public static BigDecimal getDecimal(Double v1) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        return b1.setScale(2, BigDecimal.ROUND_DOWN);
    }

    /**
     * 加法
     */
    public static BigDecimal add(BigDecimal v1, BigDecimal v2) {
        BigDecimal multiply = v1.add(v2);
        return multiply.setScale(2, BigDecimal.ROUND_DOWN);
//        return b1.add(b2);
    }

    /**
     * 加法
     */
    public static BigDecimal add(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        BigDecimal multiply = b1.add(b2);
        return multiply.setScale(2, BigDecimal.ROUND_DOWN);
//        return b1.add(b2);
    }

    /**
     * 减法
     */
    public static BigDecimal sub(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        BigDecimal multiply = b1.subtract(b2);
        return multiply.setScale(2, BigDecimal.ROUND_DOWN);
//        return b1.subtract(b2);
    }

    /**
     * 乘法
     */
    public static BigDecimal mul(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        System.out.println("乘法 " + b1 + "     " + b2);
        BigDecimal multiply = b1.multiply(b2);
        return multiply.setScale(2, BigDecimal.ROUND_DOWN);
    }

    /**
     * 乘法
     */
    public static BigDecimal mul0(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        System.out.println("乘法 " + b1 + "     " + b2);
        BigDecimal multiply = b1.multiply(b2);
        return multiply.setScale(0, BigDecimal.ROUND_DOWN);
    }


    /**
     * 除法
     */
    public static BigDecimal div(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        System.out.println("除法 " + b1 + "     " + b2);
        return b1.divide(b2, 2, BigDecimal.ROUND_DOWN);// 四舍五入,保留2位小数
        // 除不尽的情况
    }


}
